package com.movie.utils;

import com.movie.common.HttpResult;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author Liu Jiaquan
 * @Description
 * @date 2024/7/22 14:38
 */
@Component
public class FileUpload {
    @Value("${hw.bucket-name}")
    private String hwEndBuckName;
    @Autowired
    private ObsClient obsClient;

    public String upload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        String hzName = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + hzName;
        try {
            // 上传文件到OBS
            obsClient.putObject(new PutObjectRequest(hwEndBuckName, fileName, file.getInputStream()));
            return fileName;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
